package li.nibble.twittertido;

import java.util.ArrayList;

import li.nibble.twittertido.adapter.CommentAdapter;
import li.nibble.twittertido.beans.Comment;
import li.nibble.twittertido.beans.Tweet;
import li.nibble.twittertido.lazyimage.ImageLoader;
import li.nibble.twittertido.model.DataBase;
import li.nibble.twittertido.webservice.Client;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class TweetView extends ListActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tweet);

		initAds();

		final String username = DataBase.getPreference(getApplicationContext(),
				DataBase.PREFERENCES_NAME_USERNAME);

		try {

			final Tweet tweet = DataBase.getTweet(this, this.getIntent()
					.getExtras().getInt("id_tweet"));

			final ArrayList<Comment> comments = Client.getComments(this,
					tweet.getId());

			ImageView image = (ImageView) findViewById(R.id.avatar_user);
			TextView tweet_s = (TextView) findViewById(R.id.tweet);
			TextView user = (TextView) findViewById(R.id.user);
			// LinearLayout options = (LinearLayout)
			// findViewById(R.id.tweet_options);
			ImageView comment = (ImageView) findViewById(R.id.option_reply);
			final ImageView like = (ImageView) findViewById(R.id.option_like);
			final ImageView dislike = (ImageView) findViewById(R.id.option_dislike);
			final ImageView share = (ImageView) findViewById(R.id.option_share);

			if (tweet.getLikes() > 0) {
				like.setImageResource(R.drawable.ic_like_green);
				dislike.setImageResource(R.drawable.ic_dislike);
			} else if (tweet.getLikes() < 0) {
				dislike.setImageResource(R.drawable.ic_dislike_red);
				like.setImageResource(R.drawable.ic_like);
			} else {
				like.setImageResource(R.drawable.ic_like);
				dislike.setImageResource(R.drawable.ic_dislike);
			}

			like.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					like(tweet, like);
				}
			});

			dislike.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					dislike(tweet, dislike);
				}
			});

			comment.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					final Dialog dialog = new Dialog(TweetView.this,
							android.R.style.Theme_Translucent_NoTitleBar);

					dialog.setContentView(R.layout.send_comment);

					LayoutParams lp = dialog.getWindow().getAttributes();
					lp.x = 0;
					lp.y = 0;
					lp.width = WindowManager.LayoutParams.FILL_PARENT;
					;
					lp.height = WindowManager.LayoutParams.FILL_PARENT;
					;
					lp.gravity = Gravity.TOP | Gravity.LEFT;
					lp.dimAmount = 0;
					lp.flags = LayoutParams.FLAG_LAYOUT_NO_LIMITS
							| LayoutParams.FLAG_NOT_TOUCH_MODAL;
					dialog.getWindow().setAttributes(lp);

					final EditText username_ed = (EditText) dialog
							.findViewById(R.id.username);

					if (username == null || username.length() <= 0) {
						username_ed.setVisibility(View.VISIBLE);
					}else{
						username_ed.setVisibility(View.GONE);
					}

					final Button cancel = (Button) dialog
							.findViewById(R.id.cancel);
					cancel.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View arg0) {
							dialog.cancel();
						}

					});

					final EditText comment_te = (EditText) dialog
							.findViewById(R.id.comment);

					Button send = (Button) dialog.findViewById(R.id.send);

					send.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View arg0) {
							if (comment_te.getText().toString().length() > 0) {
								if (username == null || username.length() <= 0) {
									DataBase.setPreference(
											getApplicationContext(),
											DataBase.PREFERENCES_NAME_USERNAME,
											username_ed.getText().toString());
								}

								Comment new_comment = new Comment(
										0,
										DataBase.getPreference(
												getApplicationContext(),
												DataBase.PREFERENCES_NAME_USERNAME),
										comment_te.getText().toString());

								Client.comment(getApplicationContext(),
										tweet.getId(),
										new_comment.getUsername(),
										new_comment.getComment());
								comments.add(0, new_comment);
								CommentAdapter ca = new CommentAdapter(
										TweetView.this, R.layout.comment_row,
										comments);

								setListAdapter(ca);
								dialog.cancel();
							}

						}
					});

					// TextView text = (TextView)
					// dialog.findViewById(R.id.text);
					// text.setText("Hello, this is a custom dialog!");
					// ImageView image = (ImageView)
					// dialog.findViewById(R.id.image);
					// image.setImageResource(R.drawable.android);
					dialog.show();

				}
			});

			share.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent sharingIntent = new Intent(Intent.ACTION_SEND);
					sharingIntent.setType("text/plain");
					sharingIntent.putExtra(
							android.content.Intent.EXTRA_SUBJECT, v
									.getContext()
									.getString(R.string.shared_via));
					
					String share_text = tweet.getTweet();
					if (share_text.length() > 119) {
						share_text = share_text.substring(0, 116) + "...";
					}

					share_text += " http://twittertido.com.es/tweet.php?id=" + tweet.getId();

					sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, share_text);
					v.getContext().startActivity(
							Intent.createChooser(sharingIntent, v.getContext()
									.getString(R.string.share_it)));
				}
			});

			ImageLoader imageLoader = new ImageLoader(this);

			imageLoader.DisplayImage(
					"https://api.twitter.com/1/users/profile_image?screen_name="
							+ tweet.getUser() + "&size=normal", image);

			tweet_s.setText(tweet.getTweet());
			user.setText(tweet.getUser());

			CommentAdapter ca = new CommentAdapter(this, R.layout.comment_row,
					comments);

			setListAdapter(ca);
		} catch (Exception e) {
			Log.e(TweetView.class.getName(), e.getLocalizedMessage());
		}

	}

	public void like(Tweet tweet, View v) {
		if (tweet.getLikes() == 0) {
			Client.like(this, tweet.getId(), Tweet.LIKE);
			tweet.setLikes(Tweet.LIKE);
		}

		if (tweet.getLikes() == Tweet.LIKE) {
			((ImageView) v).setImageResource(R.drawable.ic_like_green);
		} else if (tweet.getLikes() == Tweet.DISLIKE) {
			((ImageView) v).setImageResource(R.drawable.ic_like);
		}
	}

	public void dislike(Tweet tweet, View v) {
		if (tweet.getLikes() == 0) {
			Client.like(this, tweet.getId(), Tweet.DISLIKE);
			tweet.setLikes(Tweet.DISLIKE);
		}

		if (tweet.getLikes() == Tweet.DISLIKE) {
			((ImageView) v).setImageResource(R.drawable.ic_dislike_red);
		} else if (tweet.getLikes() == Tweet.DISLIKE) {
			((ImageView) v).setImageResource(R.drawable.ic_dislike);
		}
	}

	private void initAds() {
		AdView adView = new AdView(this, AdSize.BANNER, Constants.ADMOB);
		LinearLayout layout = (LinearLayout) findViewById(R.id.ads);
		layout.addView(adView);
		adView.loadAd(new AdRequest());
	}

}
